package com.newviews.tcmip.web.controller;

import com.newviews.tcmip.common.config.ConfigProperties;
import com.newviews.tcmip.common.constant.CommonConsts;
import com.newviews.tcmip.common.utils.ExportExcelUtils;
import com.newviews.tcmip.common.utils.UUIDGenerator;
import com.newviews.tcmip.pojo.po.EtcmUiElPo;
import com.newviews.tcmip.pojo.vo.export.EtcmCommonExportReqVO;
import com.newviews.tcmip.pojo.vo.export.EtcmPxNetworkExportReqVO;
import com.newviews.tcmip.service.inf.EtcmDiseaseExServiceInf;
import com.newviews.tcmip.service.inf.EtcmHerbExServiceInf;
import com.newviews.tcmip.web.vo.etcmherb.download.*;
import com.newviews.tcmip.web.vo.etcmingr.EtcmTargetExportReqVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 中草药库 前端控制器
 * </p>
 *
 * @author lxc
 * @since 2024-02-29
 */
@RestController
@RequestMapping("/etcm/herb/download")
@Api(tags = "中草药库下载")
public class EtcmHerbDownloadController {
    @Resource
    private EtcmHerbExServiceInf etcmHerbExServiceInf;
    @Resource
    private EtcmDiseaseExServiceInf etcmDiseaseExServiceInf;
    @Resource
    private ConfigProperties configProperties;

    @PostMapping("")
    @ApiOperation(value = "中草药库：下载",notes="herb")
    public void downloadHerb(
            @ApiIgnore HttpServletResponse response,
            @RequestBody @Valid EtcmHerbExportReqVO reqVO) {
        try {
            List<Map<String, String>> list = etcmHerbExServiceInf.downloadHerb(reqVO);

            etcmDiseaseExServiceInf.getDownload(response,reqVO.getUiTypeCode(),reqVO.getLangCode(),reqVO.getExportType(),list);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    @PostMapping("/details")
    @ApiOperation(value = "中草药库：详情下载",notes="Basic information：herb information")
    public void downloadHerbDetails(
            @ApiIgnore HttpServletResponse response,
            @RequestBody @Valid EtcmHerbDetailsExportReqVO reqVO) {
        try {
            List<Map<String, String>> pxDetails = etcmHerbExServiceInf.downloadHerbDetails(reqVO);
            commonDownload(response, reqVO, pxDetails);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {

        }
    }



    @PostMapping("/ingr")
    @ApiOperation(value = "中草药构成: 相关成分下载",notes="herb composition: ingredient")
    public void downloadHerbIngr(
            @ApiIgnore HttpServletResponse response,
            @RequestBody @Valid EtcmHerbByIngrExportReqVO reqVO) {
        try {
            List<Map<String, String>> pxDetails = etcmHerbExServiceInf.downloadHerbIngr(reqVO);
            //commonDownload(response, reqVO, pxDetails);
            etcmDiseaseExServiceInf.getDownload(response,CommonConsts.UI_TYPE_CODE_HERB_INGR,reqVO.getLangCode(),reqVO.getExportType(),pxDetails);


        } catch (Exception e) {
            e.printStackTrace();
        } finally {

        }
    }

    @PostMapping("/ingr/target")
    @ApiOperation(value = "中草药构成: 成分相关靶向下载",notes="herb composition: target")
    public void downloadHerbTarget(
            @ApiIgnore HttpServletResponse response,
            @RequestBody @Valid EtcmHerbTargetExportReqVO reqVO) {
        try {
            List<Map<String, String>> pxDetails = etcmHerbExServiceInf.downloadHerbTarget(reqVO.getHerbId(),reqVO.getContent(),reqVO.getUiTypeCode(),reqVO.getLangCode(),reqVO.getSortType(),reqVO.getType());
            etcmDiseaseExServiceInf.getDownload(response, reqVO.getUiTypeCode(), reqVO.getLangCode(),reqVO.getExportType(),pxDetails);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {

        }
    }


    @PostMapping("/experimental/records")
    @ApiOperation(value = "中草药组学信息: 下载",notes="omics data records: experimental records")
    public void downloadExperimentalRecords(
            @ApiIgnore HttpServletResponse response,
            @RequestBody @Valid EtcmHerbDetailsExportReqVO reqVO) {
        try {
            List<Map<String, String>> pxDetails = etcmHerbExServiceInf.downloadExperimentalRecords(reqVO.getHerbId(),reqVO.getContent(),reqVO.getUiTypeCode(),reqVO.getLangCode(),reqVO.getSortType());
            etcmDiseaseExServiceInf.getDownload(response, reqVO.getUiTypeCode(), reqVO.getLangCode(),reqVO.getExportType(),pxDetails);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {

        }
    }

    @PostMapping("/combined/herbs")
    @ApiOperation(value = "中草药组合用药关系：结合中草药下载",notes="combinational landscape: combined herbs")
    public void downloadCombinedHerbs(
            @ApiIgnore HttpServletResponse response,
            @RequestBody @Valid EtcmHerbByHerbExportReqVO reqVO) {
        try {
            List<Map<String, String>> list = etcmHerbExServiceInf.downloadCombinedHerbs(reqVO.getHerbId(),reqVO.getSortType(),reqVO.getContent(),reqVO.getLangCode(),reqVO.getUiTypeCode(),reqVO.getContent());

            etcmDiseaseExServiceInf.getDownload(response,reqVO.getUiTypeCode(),reqVO.getLangCode(),reqVO.getExportType(),list);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @PostMapping("/associated/diseases")
    @ApiOperation(value = "中草药组合用药关系: 相关的疾病下载",notes="combinational landscape: associated diseases")
    public void downloadAssociatedDiseases(
            @ApiIgnore HttpServletResponse response,
            @RequestBody @Valid EtcmHerbByHerbExportReqVO reqVO) {
        try {
            List<Map<String, String>> list = etcmHerbExServiceInf.downloadAssociatedDiseases(reqVO);
            etcmDiseaseExServiceInf.getDownload(response,reqVO.getUiTypeCode(),reqVO.getLangCode(),reqVO.getExportType(),list);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    @PostMapping("/px")
    @ApiOperation(value = "中草药: 相关处方下载",notes="Related Prescription")
    public void downloadPx(
            @ApiIgnore HttpServletResponse response,
            @RequestBody @Valid EtcmHerbDetailsExportReqVO reqVO) {
        try {
            List<Map<String, String>> pxDetails = etcmHerbExServiceInf.downloadPx(reqVO);
            //commonDownload(response, reqVO, pxDetails);
            etcmDiseaseExServiceInf.getDownload(response,reqVO.getUiTypeCode(),reqVO.getLangCode(),reqVO.getExportType(),pxDetails);

        } catch (Exception e) {
            e.printStackTrace();
        } finally {

        }
    }


//    @PostMapping("/network")
//    @ApiOperation("中草药network图下载")
//    public void networkStatistics( @ApiIgnore HttpServletResponse response,@RequestBody EtcmHerbNetworkExportReqVO reqVO){
//        String path="";
//        try {
//            List<Map<String, String>> pxDetails = new ArrayList<>();
//            File file=new File(configProperties.getFileExportPath());
//            if(!file.exists()){
//                file.mkdirs();
//            }
//            if(reqVO.getExportType().equals(CommonConsts.STR_COMMON_YES)){
//                path= configProperties.getFileExportPath()+ UUIDGenerator.getDateNumber()+CommonConsts.EXPORT_FILE_FORMAT_1;
//                ExportExcelUtils.writeListOfMapsToCSV(pxDetails, path);
//            }else {
//                path= configProperties.getFileExportPath()+ UUIDGenerator.getDateNumber()+CommonConsts.EXPORT_FILE_FORMAT_2;
//                ExportExcelUtils.writeListOfMapsToTxt(pxDetails, path);
//            }
//            InputStream stream = new FileInputStream(path);
//            ServletOutputStream out = response.getOutputStream();
//            byte buff[] = new byte[1024];
//            int length = 0;
//            while ((length = stream.read(buff)) > 0) {
//                out.write(buff, 0, length);
//            }
//            stream.close();
//            out.close();
//            out.flush();
//        } catch (IOException e) {
//            e.printStackTrace();
//        } finally {
//            new File(path).delete();
//        }
//
//    }


    private void commonDownload(@ApiIgnore HttpServletResponse response, @RequestBody @Valid EtcmCommonExportReqVO reqVO, List<Map<String, String>> pxDetails) throws IOException {
        String path;
        String filename = "";
        File file=new File(configProperties.getFileExportPath());
        if(!file.exists()){
            file.mkdirs();
        }
        if(reqVO.getExportType().equals(CommonConsts.STR_COMMON_YES)){
            filename = UUIDGenerator.getDateNumber()+CommonConsts.EXPORT_FILE_FORMAT_1;
            path= configProperties.getFileExportPath()+ filename;
            ExportExcelUtils.writeListOfMapsToCSV(pxDetails, path);
        }else {
            filename = UUIDGenerator.getDateNumber()+CommonConsts.EXPORT_FILE_FORMAT_2;
            path= configProperties.getFileExportPath()+ filename;
            ExportExcelUtils.writeListOfMapsToTxt(pxDetails, path);
        }
        InputStream stream = new FileInputStream(path);
        response.setHeader("Content-Disposition", "attachment;"+"filename=" + new String(filename.getBytes(), "utf-8"));
        ServletOutputStream out = response.getOutputStream();
        byte[] buff = new byte[1024];
        int length = 0;
        while ((length = stream.read(buff)) > 0) {
            out.write(buff, 0, length);
        }
        stream.close();
        out.close();
        out.flush();
    }


}
